<template>
    <div id='' class='system_set_form'>
        <i-form ref="iForm" :config="config">
        </i-form>
    </div>
</template>
<script>
import { config } from './form_config'
import { mapState } from 'vuex'
export default {
    name: '',
    props: ['isInit'],
    data() {
        this.newConfig = icu.util.deepClone(config)
        return {
            config: this.newConfig,
            formData: {},
            sort: 1
        };
    },
    components: {},
    computed: {
        ...mapState('extend/sysDepartment', ['$row'])
    },
    beforeMount() { },
    mounted() {
        this.getXzqList();
        if (this.isInit) {
            this.getUpdepartList(null);
        }
    },
    methods: {
        setUpMenu() {
            this.$nextTick(() => {
                for (let i = 0; i < this.config.form.length; i++) {
                    let element = this.config.form[i]
                    if (element.key == 'pid') {
                        element.disabled = true
                    }
                }
                this.$refs.iForm.setData({
                    pid: this.$row.pid == null ? '0' : this.$row.id,
                })
            })
        },
        getForm(cb) {
            this.$refs.iForm.submit('departmentAddUpdata', (res) => {
                this.formData = this.returnData(res)
                this.$http.$post('deptSave', { data: { ...this.formData, ...{ region: this.formData.region[this.formData.region.length - 1] } } }).then(res => {
                    if (res.code == 200) {
                        this.$Message.success('保存成功');
                        if (cb) {
                            cb(this.formData)
                        }
                        return
                    }
                    this.$Message.error(res.msg);
                })
            })
        },
        returnData(res) {
            return {
                id: res.id ? res.id : '',
                mobile: res.mobile ? res.mobile : '',
                name: res.name ? res.name : '',
                organ: res.organ ? res.organ : '',
                pid: res.pid ? res.pid : '',
                sort: res.sort,
                region: res.region ? res.region : [],
                xzqbs: res.xzqbs
            }
        },
        setData(row, value) {
            this.reset()
            this.$http.$get('deptDetail', { params: { id: row.id } }).then(res => {
                if (res.code == 200) {
                    res.data.region = res.data.regionValues.split(',')
                    this.$refs.iForm.setData({
                        ...this.returnData(res.data)
                    })
                    return
                }
                this.$Message.error(res.msg);
            })
            if (value == '编辑') {
                for (let i = 0; i < this.config.form.length; i++) {
                    let element = this.config.form[i]
                    if (element.key == 'pid') {
                        element.disabled = true
                    }
                }
            }
            this.getUpdepartList(row.id)
        },
        // 获取行政区数据
        getXzqList() {
            this.$http.$get('deptRegionTree', { params: {} }).then(res => {
                if (res.code == 200) {
                    let index = this.config.form.findIndex(item => item.key == 'region');
                    this.config.form[index].option = res.data;
                    return
                }
                this.$Message.error(res.msg);
            })
        },
        getUpdepartList(id) {
            this.$http.$get('deptTopTree', { params: {} }).then(res => {
                if (res.code == 200) {
                    for (let i = 0; i < this.config.form.length; i++) {
                        const element = this.config.form[i];
                        if (element.key == 'pid') {
                            this.$set(res.data[0], 'expand', true)
                            element.data = id ? this.setTreeData(res.data, id) : res.data
                        }
                    }
                    return
                }
                this.$Message.error(res.msg);
            })
        },
        reset() {
            this.$refs.iForm.reset('departmentAddUpdata')
            if (this.isInit) {
                this.getUpdepartList(null);
            }
        },
        setTreeData(data, id) {
            for (let i = 0; i < data.length; i++) {
                if (data[i].id == id) {
                    data.splice(i, 1)
                    return data;
                } else if (data[i].children && data[i].children.length > 0) {
                    this.setTreeData(data[i].children, id)
                }
            }
            return data;
        },
    }
}
</script>
<style lang='less' scoped>
</style>
